package com.panshi.controller;

import com.panshi.entity.*;
import com.panshi.result.R;
import com.panshi.service.SysClientService;
import com.panshi.utils.AdminInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminSysController {

    @Autowired
    private AdminInfo adminInfo;

    @Autowired
    private SysClientService sysClientService;

    @GetMapping("/sys/list")
    public String sysList(HttpServletRequest request, HttpSession session, Model model){
        adminInfo.sysList(request,model,session,"sysList");
        return "/sys/list";
    }


    @GetMapping("/sys/adminAddRole/{adminId}")
    public String adminAddRole(@PathVariable int adminId,Model model){
        SysAdminT adminT = sysClientService.getAdminById(adminId);
        if(adminT==null){
            return "404.html";
        }
        model.addAttribute("adminT",adminT);
        List<SysRoleT> sysRoleTs = sysClientService.roleAll();
        model.addAttribute("sysRoleTs",sysRoleTs);
        System.out.println("添加角色");
        System.out.println(adminT);
        return "/sys/adminAddRole";
    }

    @GetMapping("/sys/userRoleDel")
    @ResponseBody
    public R userRoleDel(HttpServletRequest request){
        String adminId = request.getParameter("adminId");
        String rid = request.getParameter("rid");
        R r = new R();
        if(adminId==null){
            return R.error();
        }
        if (rid==null){
            return R.error();
        }
        System.out.println(adminId+"  删除 "+rid);
        Boolean aBoolean = sysClientService.sysUserDelRole(new SysUserRoleT().setAdminId(Integer.valueOf(adminId)).setRoleId(Integer.valueOf(rid)));
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(true);
            r.setMessage("删除成功");
            return r;
        }
        return R.error();
    }

    @PostMapping("/sys/adminAddRole/{adminId}")
    @ResponseBody
    public R adminAddRolem(HttpServletRequest request,@PathVariable int adminId){
        String rid = request.getParameter("roleid");
        System.out.println(rid+"   "+adminId);
        R r = new R();
        SysUserRoleT sysUserRoleT1 = sysClientService.userRole(adminId);
        if(sysUserRoleT1!=null){
           r.setCode(301);
           r.setSuccess(false);
           r.setMessage("管理员已存在角色");
           return r;
        }
        if(rid==null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("系统异常");
            return r;
        }
        SysUserRoleT sysUserRoleT = new SysUserRoleT();
        sysUserRoleT.setAdminId(adminId);
        sysUserRoleT.setRoleId(Integer.valueOf(rid));
        Boolean aBoolean = sysClientService.sysUserAddRole(sysUserRoleT);
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(false);
            r.setMessage("添加成功");
            return r;
        }
        return R.error();
    }

    @GetMapping("/sys/role")
    public String sysRole(HttpServletRequest request,HttpSession session,Model model){
        System.out.println("角色管理  --- 》");
        adminInfo.roleList(request,model,session,"sysRole");
        return "/sys/role";
    }

    @GetMapping("/sys/roleDel/{rid}")
    public String roleDel(@PathVariable("rid") int rid,Model model){
        System.out.println("删除权限  roleDel   "+rid);
        adminInfo.getByRole(rid);
        SysRoleT role = adminInfo.getByRole(rid);
        model.addAttribute("role",role);
        System.out.println(role);
        return "/sys/roleDel";
    }

    @GetMapping("/sys/fundel")
    @ResponseBody
    public R fundel(HttpServletRequest request){
        String funid = request.getParameter("funid");
        R r = new R();
        if(funid==null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("系统异常");
            return r;
        }
        System.out.println(funid);
        Boolean aBoolean = sysClientService.funByIdDel(Integer.valueOf(funid));
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(aBoolean);
            r.setMessage("删除成功");
            return r;
        }
        return R.error();
    }


    @GetMapping("/sys/function")
    public String sysFunction(HttpServletRequest request,HttpSession session,Model model){
        adminInfo.funLimitList(request,model,session,"fun");
        return "/sys/function";
    }

    @GetMapping("/sys/funAdd")
    public String funAdd(){
        return "/sys/funAdd";
    }

    @PostMapping("/sys/funAdd")
    @ResponseBody
    public R funadd(HttpServletRequest request){
        String funname = request.getParameter("funname");
        String funurl = request.getParameter("funurl");
        R r = new R();
        if(funname==null){
            r.setCode(501);
            r.setSuccess(false);
            r.setMessage("请输入权限名");
            return r;
        }
        if(funurl==null){
            r.setCode(501);
            r.setSuccess(false);
            r.setMessage("请输入权限url");
            return r;
        }

        SysFunctionT funname1 = sysClientService.funname(funname);
        System.out.println("权限"+(funname1!=null));
        if(funname1!=null){
            r.setCode(501);
            r.setSuccess(false);
            r.setMessage("已存在该权限名");
            return r;
        }
        SysFunctionT funurl1 = sysClientService.funurl(funurl);
        if(funurl1!=null){
            r.setCode(501);
            r.setSuccess(false);
            r.setMessage("已存在该权限url");
            return r;
        }


        Boolean aBoolean = sysClientService.funAdd(new SysFunctionT().setFunctionName(funname).setFunctionUrl(funurl).setFunctionPid("1"));
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(true);
            r.setMessage("添加成功   ---- 请重新进入页面！！！");
            return r;
        }
        r.setCode(501);
        r.setSuccess(false);
        r.setMessage("系统繁忙");
        return r;
    }


    @PostMapping("/sys/roleDel/{rid}")
    @ResponseBody
    public Boolean fundel(@PathVariable("rid") int rid,Model model,HttpServletRequest request){
        System.out.println("删除权限  roleDel   "+rid);
        String[] array = request.getParameterValues("array[]");
        if(array==null){
            return false;
        }
        //添加权限
        SysRoleFunctionT roleFun = new SysRoleFunctionT();
        roleFun.setRoleId(rid);
        boolean flag = false;
        for (String funId : array) {
            System.out.println("删除权限ID    "+funId+"角色 ID  -- "+rid);
            roleFun.setFunctionId(Integer.valueOf(funId));
            flag = sysClientService.roleFunDel(roleFun);
        }
        System.out.println("结果 --------》"+flag);
        return flag;
    }

    @GetMapping("/sys/roleadd/{rid}")
    public String roleadd(@PathVariable("rid") int rid,Model model){
        System.out.println("添加权限  roleadd   "+rid);
        SysRoleT role = adminInfo.getIsNotRole(rid);
        model.addAttribute("role",role);
        System.out.println(role);
        return "/sys/roleadd";
    }


    @PostMapping("/sys/roleadd/{rid}")
    @ResponseBody
    public Boolean funadd(@PathVariable("rid") int rid,HttpServletRequest request){
        String[] array = request.getParameterValues("array[]");
        if(array==null){
            return false;
        }
        //添加权限
        SysRoleFunctionT roleFun = new SysRoleFunctionT();
        roleFun.setRoleId(rid);
        boolean flag = false;
        for (String funId : array) {
            System.out.println("添加权限ID"+funId+"角色 ID  -- "+rid);
            roleFun.setFunctionId(Integer.valueOf(funId));
            flag = sysClientService.roleFunAdd(roleFun);
        }
        System.out.println("结果 --------》"+flag);
        return flag;
    }

    @GetMapping("/sys/cate")
    public String sysCate(){
        return "/sys/cate";
    }


    @GetMapping("/sys/rule")
    public String sysRule(){
        return "/sys/rule";
    }

    @GetMapping("/sys/edit")
    public String sysEdit(){
        return "/sys/edit";
    }
    @GetMapping("/sys/add")
    public String sysAdd(){
        return "/sys/add";
    }
}
